package com.controller;

import com.alibaba.fastjson.JSON;
import com.pojo.User;
import com.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.UUID;

@Controller
public class RoleController {


    @Resource
    private UserService userService;

    /*@Resource
        private RestTemplate template;
    */

    @RequestMapping(value = "/toUpload")
    public String toUpload(String userName, String password,
                           @RequestParam("picPath") MultipartFile multipartFile,
                           HttpServletRequest request) throws IOException {

        String path=request.getSession().getServletContext().getRealPath("/upload");
        File file1 = new File(path);
        if(!file1.exists()){
            file1.mkdir();
        }
        String fileName = UUID.randomUUID().toString() + multipartFile.getOriginalFilename();
        File file = new File(path,fileName);
        multipartFile.transferTo(file);

        return "welcome";
    }



    @RequestMapping(value = "/getUserAjax")
    @ResponseBody
    public User getUserAjax(@RequestBody User user, HttpServletResponse response) throws IOException {
        //response.setCharacterEncoding("UTF-8");
        User userOne = userService.getUserOne(2);
        //String string = JSON.toJSONString(userOne);
        return userOne;
    }

    @RequestMapping(value = "/getUser/{id}/{name}")
    public String getUser(Model model,@PathVariable("id") int id,@PathVariable("name") String name){
        System.out.println(name);
       /* User userOne = userService.getUserOne(id);
        if(userOne!=null){
            throw  new  RuntimeException("测试局部异常...");
        }*/
        return "welcome";
    }
/*
    @RequestMapping(value = "/getUser",method = RequestMethod.POST)
    public String getUser1(Model model,int id){
        User userOne = userService.getUserOne(id);
        if(userOne!=null){
            throw  new  RuntimeException("测试局部异常...");
        }
        return "welcome";
    }*/

    /*
    * Controller--->View
    *   Model,map,request,session,ModelAndView
    *
    * redirect:/index：重定向
    * forward:welcome：转发
    * */
    @RequestMapping("/index")
    public String index(Model model, Map map, HttpServletRequest request){
        /*--------*/
        model.addAttribute("name","lisi");
        map.put("age",12);
        request.setAttribute("sex","男");

        HttpSession session = request.getSession();
        session.setAttribute("passwor","123456");
        return "redirect:/toWelcome";
    }

    @RequestMapping("/toWelcome")
    public String toWelcome(){

        return "welcome";
    }

    /*
    * View--->Controller
    *  1:request.getParameter("name");
    *  2:@ModelAttribute,@RequestParam(value = "name",required = false) String name
    *  3:直接映射
    * */
    @RequestMapping("/paremtertoController")
    public String paremtertoController(String name, HttpServletRequest request){
        String name1 = request.getParameter("name");
        System.out.println(name1);
        return "welcome";
    }

    @RequestMapping("/tofome")
    public String tofome(User user, HttpServletRequest request){
        String name1 = request.getParameter("name");
        System.out.println(name1);
        return "welcome";
    }

    //局部异常处理（备选方案）
    /*@ExceptionHandler(value = RuntimeException.class)
    public String exceptionErro(HttpServletRequest request ,RuntimeException e){
        request.setAttribute("e",e.getMessage());
        return "errors";
    }*/



}
